package com.tcc.task;

import org.apache.xpath.operations.String;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.tcc.qa.test.util.BaseTest;
import com.tcc.qa.test.util.ButtonFinder;
import com.tcc.qa.test.util.MsgFinder;
import com.tcc.qa.test.util.SelectFinder;

public class CategoryManager extends BaseTest {

  String categoryName1 = "Work";

  String categoryName2 = "Study";

  @BeforeMethod
  public void beforeTestMethod() {
    System.out.println("Before Test Method");
  }

  @Test(description = "Click Personal Task Icon and go to My Tasks Page", priority = 1)
  public void taskLinkIcon() throws InterruptedException {
	  getWebDriver().findElement(By.id("qbtasks")).click();
    Assert.assertEquals(getWebDriver().getTitle().toString(), "My Tasks @ MyCruiser>Personal Tools");
  }

  @Test(description = "Add category without name", priority = 2)
  public void add_nullName() {
	ButtonFinder.getTableButton(getWebDriver(), "Categories").click();  //Click Categories tab
    ButtonFinder.getButtonByTitle(getWebDriver(), "Add").click();
    getWebDriver().switchTo().alert().sendKeys("");
    getWebDriver().switchTo().alert().accept();
    Assert.assertEquals(getWebDriver().switchTo().alert().getText(), "You must enter a name.");
    getWebDriver().switchTo().alert().accept();
  }

  @Test(description = "Add new category", priority = 3)
  public void add_Category() {
    ButtonFinder.getButtonByTitle(getWebDriver(), "Add").click();
    getWebDriver().switchTo().alert().sendKeys(categoryName1);
    getWebDriver().switchTo().alert().accept();
    Assert.assertEquals(SelectFinder.getSelectText(getWebDriver(), "categories", 0), categoryName1);
  }

  @Test(description = "Edit the category with an exist name", priority = 4)
  public void edit_sameName(){
    SelectFinder.getSelectOption(getWebDriver(), "categories", 0).click();
    ButtonFinder.getButtonByTitle(getWebDriver(), "Rename").click();
    getWebDriver().switchTo().alert().sendKeys(categoryName1);
    getWebDriver().switchTo().alert().accept();
    Assert.assertEquals(getWebDriver().switchTo().alert().getText(), "Entry already available.  Please choose another name.");
    getWebDriver().switchTo().alert().accept();
 
  }

  @Test(description = "Rename the category", priority = 5)//There is an error here
  public void edit_Rename() {
	  SelectFinder.getSelectOption(getWebDriver(), "categories", 0).click();
    ButtonFinder.getButtonByTitle(getWebDriver(), "Rename").click();
    getWebDriver().switchTo().alert().sendKeys(categoryName2);
    getWebDriver().switchTo().alert().accept();
    String text = SelectFinder.getSelectText(getWebDriver(), "categories",0);
    Assert.assertEquals(text , categoryName2);
  }
  @Test(description = "Save the changes and check the successful message", priority = 7)
  public void save_message() {
    ButtonFinder.getInputButtonByValue(getWebDriver(), "Save").click();
    Assert.assertEquals(MsgFinder.getMsgDiv(getWebDriver()).getText(), "Categories have been saved successfully.");
  }

  @Test(description = "check whether saved or not", priority = 8)
  public void save_check() {
    Assert.assertEquals(SelectFinder.getSelectText(getWebDriver(), "categories", 0), categoryName2);
  }

  @Test(description = "Reset after add a new category", priority = 9)
  public void Reset() {
    ButtonFinder.getButtonByTitle(getWebDriver(), "Add").click();
    getWebDriver().switchTo().alert().sendKeys(categoryName1);
    getWebDriver().switchTo().alert().accept();
    ButtonFinder.getInputButtonByValue(getWebDriver(), "Reset").click();

    Assert.assertEquals(SelectFinder.getSelectText(getWebDriver(), "categories", 0), categoryName2);
  }

  @Test(description = "Cancel after add a new Category", priority = 10)
  public void Cancle() {
    ButtonFinder.getButtonByTitle(getWebDriver(), "Add").click();
    getWebDriver().switchTo().alert().sendKeys(categoryName1);
    getWebDriver().switchTo().alert().accept();
    ButtonFinder.getInputButtonByValue(getWebDriver(), "Cancel").click();
    Assert.assertEquals(getWebDriver().getTitle().toString(), "My Tasks @ MyCruiser>Personal Tools");
    ButtonFinder.getTableButton(getWebDriver(), "Categories").click();  
    Assert.assertEquals(SelectFinder.getSelectText(getWebDriver(), "categories", 0), categoryName2);
    System.out.println("Now there is a category which named study");
  }

  @AfterMethod
  public void afterMethod() {
    System.out.println("End the method test.");

  }

  @AfterTest
  public void endTest() {
    System.out.println("End the test.");

  }

}
